package customerService

import (
	"context"

	"gitee.com/liuxuezhan/ar-platform/admin-api/pkg/code"
	"gitee.com/liuxuezhan/ar-platform/rpc-client/setting/client/customerservicegroup"
	"gitee.com/liuxuezhan/ar-platform/rpc-client/setting/rpc"
	"github.com/zeromicro/go-zero/core/logc"

	"gitee.com/liuxuezhan/ar-platform/admin-api/internal/svc"
	"gitee.com/liuxuezhan/ar-platform/admin-api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type AddCustomerServiceLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

// 新增客服管理
func NewAddCustomerServiceLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AddCustomerServiceLogic {
	return &AddCustomerServiceLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *AddCustomerServiceLogic) AddCustomerService(req *types.AddCustomerServiceReq) (resp *types.Empty, err error) {
	if req.CommunicationType.CommunicationType == 0 || req.CustomerServiceType.CustomerServiceType == 0 {
		return nil, code.ParamsInvalid
	}

	u, err := l.svcCtx.SettingService.CustomerService.AddCustomerService(l.ctx, &customerservicegroup.AddCustomerServiceReq{
		MerchantId:           1,                                                                    // 商户ID
		CustomerServiceType:  rpc.CustomerServiceType(req.CustomerServiceType.CustomerServiceType), // 客服类型
		CommunicationType:    rpc.CommunicationType(req.CommunicationType.CommunicationType),       // 通讯类型
		Title:                req.Title,                                                            // 名称
		CommunicationAddress: req.CommunicationAddress,                                             // 通讯地址
		Sort:                 req.Sort,                                                             // 排序
		Status:               rpc.StatusType(req.Status),
	})

	if err != nil {
		logc.Error(l.ctx, "AddCustomerService err:", err)
		return nil, err
	}

	if u.Code != int64(code.SettingRpcSuccessful.Code()) {
		return nil, code.BusinessFailure
	}

	resp = &types.Empty{}
	return
}
